package xiaojun.JAVA.service.array;

import xiaojun.JAVA.domain.dto.UserDto;
import xiaojun.JAVA.domain.emtity.Dept;
import xiaojun.JAVA.domain.emtity.Role;
import xiaojun.JAVA.domain.emtity.User;
import xiaojun.JAVA.domain.vo.Result;
import xiaojun.JAVA.domain.vo.UserVo;
import xiaojun.JAVA.service.IDeptService;
import xiaojun.JAVA.service.IRoleService;
import xiaojun.JAVA.service.IUserService;

/**
 * 业务层-用户
 *
 * @author 赖晓君
 * @since 2025/6/8  14:31
 */
public class UserService extends BaseService<User,UserDto,UserVo>implements IUserService {

    private final IDeptService deptService;
    private final IRoleService roleService;

    private static IUserService singleton;

    private UserService(){
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        data = new User[DATA_SIZE+1];
        temp = new UserVo[DATA_SIZE+1];
    }

    public static IUserService getInstance(){
        if(singleton==null){
            singleton = new UserService();
        }
        return singleton;
    }

    @Override
    UserVo convertToVo(User user){
        String deptName ="";
        Dept dept = deptService.findById(user.deptId);
        if(dept != null){
            deptName = dept.name;
        }
        String roleName="";
        Role role = roleService.findById(user.roleId);
        if(role!= null){
            roleName = role.name;
        }

        return new UserVo(user.getId(),user.username,user.name,user.phone,user.email,user.deptId,deptName,
                user.roleId,roleName,user.enabled);
    }

    @Override
    User createEntity(UserDto dto,int id){
        return new User(id,dto.username,dto.name,dto.phone,dto.email,dto.deptId,dto.roleId);
    }

    @Override
    void updateEntity(UserDto dto,User user){
        user.username = dto.username;
        user.name = dto.name;
        user.phone = dto.phone;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public boolean deptInUse(int deptId){
        boolean inUse = false;
        for(User user:data){
            if(user!=null&&user.deptId == deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleId){
        boolean inUse = false;
        for(User user:data){
            if(user!=null&&user.roleId == roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }



    @Override
    public Result create(UserDto dto){
        if(!deptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在指定部门");
        }
        if(!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.create(dto);

    }

    @Override
    public Result updates(UserDto dto,int id){
        if(!existsById(id)){
            return Result.failure("更新失败，不存在该数据");
        }
        if(!deptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在指定部门");
        }
        if(!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.updates(dto,id);
    }

    @Override
    public Result toggle(int id){
        if(!existsById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled?"启用成功":"停用成功",null);
    }
}
